import 'dart:io';

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:share_fit/app/components/back_btn_icon.dart';
import 'package:share_fit/app/controllers/home/exercise_records/model/exercise_type.dart';
import 'package:share_fit/app/controllers/sports/motion_end/share_end_dialog.dart';
import 'package:share_fit/app/controllers/sports/motion_end/chart_view.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/routes/home_routes.dart';
import 'package:share_fit/app/tools/color_const.dart';
import 'package:share_fit/app/tools/date_tool.dart';
import 'package:share_fit/generated/locales.g.dart';
import 'package:share_plus/share_plus.dart';
import 'package:smooth_sheets/smooth_sheets.dart';

import 'logic.dart';

class MotionEndPage extends StatelessWidget {
  MotionEndPage({super.key});

  final logic = Get.put(MotionEndLogic());
  final state = Get.find<MotionEndLogic>().state;

  final GlobalKey _globalKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    state.workoutModel = Get.arguments['sportsResult'];
    state.polyLine = Get.arguments['polyLine'];
    state.fromPage = Get.arguments['from'];

    logic.createMarkers();

    return PopScope(
        canPop: false,
        onPopInvoked: (didPop) => logic.goToSportsPage(),
        child: Scaffold(
          appBar: AppBar(
            leading: BackBtnIcon(onTap: () => logic.goToSportsPage()),
            centerTitle: true,
            title: Text(
              state.fromPage == HomeRoutes.exerciseRecordsPage ? LocaleKeys.sports_detail.ts : LocaleKeys.sports_end.ts,
              style: TextStyle(fontSize: 54.sp, color: ColorConst.btnFontColor),
            ),
            actions: [
              GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  logic.showLoading();
                  String path = '';
                  bool saveMap =
                      state.workoutModel.sportsType == null || !Type.isMotionTrail(state.workoutModel.sportsType!) || state.polyLine.points.isEmpty;
                  logic.saveWidget(!saveMap, _globalKey).then((file) {
                    logic.cancelLoading();
                    if (file != null && file.existsSync()) {
                      path = file.path;
                    }
                  }).whenComplete(() {
                    if (path != '') {
                      Share.shareXFiles([XFile(path)], text: '3+ GO').then((result) {
                        if (result.status == ShareResultStatus.success) {
                          showDialog(
                              context: context,
                              builder: (_) {
                                return SharedEndDialog();
                              });
                        }
                      });
                    }
                  });
                },
                child: Container(
                  width: 80.w,
                  height: 80.w,
                  margin: EdgeInsets.only(right: 40.w),
                  child: Image.asset('assets/images/sports/icon_sport_share.png', width: 49.w, height: 54.w),
                ),
              )
            ],
          ),
          body: Stack(
            children: [
              map(),
              LayoutBuilder(builder: (context, constraints) {
                bool notMap = logic.hiddenMap();
                final minExtent = notMap ? Extent.pixels(1.sh - 200.h) : Extent.pixels(620.h);
                final initialExtent = notMap ? Extent.pixels(1.sh - 300.h) : Extent.pixels(620.h);
                return ScrollableSheet(initialExtent: initialExtent, minExtent: minExtent, child: _expandedWidget());
              })
            ],
          ),
        ));
  }

  Widget buildSummaryInfo() {
    return logic.isNoDistanceSports()
        ? RichText(
            text: TextSpan(children: [
              TextSpan(
                text: state.workoutModel.caloriesFormat.split(' ')[0],
                style: TextStyle(fontSize: 62.sp, color: Colors.black),
              ),
              const TextSpan(text: ' '),
              TextSpan(
                text: LocaleKeys.thermal_unit_kcal.ts,
                style: TextStyle(fontSize: 32.sp, color: ColorConst.secondaryFontColor),
              ),
            ]),
          )
        : RichText(
            text: TextSpan(children: [
              TextSpan(
                text: state.workoutModel.distanceFormat().split(' ')[0],
                style: TextStyle(fontSize: 62.sp, color: Colors.black),
              ),
              const TextSpan(text: ' '),
              TextSpan(
                text: logic.getLengthUnitString(),
                style: TextStyle(fontSize: 32.sp, color: ColorConst.secondaryFontColor),
              ),
            ]),
          );
  }

  Widget _expandedWidget() {
    String name = logic.read("space_person_name", defaultValue: '', global: true);
    String avatar = logic.read('space_avatar_path', defaultValue: '', global: true);
    return GetBuilder<MotionEndLogic>(
      id: 'sports_end',
      builder: (_) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Expanded(
              child: SingleChildScrollView(
                physics: const ClampingScrollPhysics(),
                child: RepaintBoundary(
                  key: _globalKey,
                  child: Column(
                    children: [
                      Container(
                        margin: EdgeInsets.only(left: 38.w, top: 38.h, right: 38.w),
                        padding: EdgeInsets.only(top: 40.h),
                        decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(40.r)),
                        child: Column(
                          children: [
                            Container(
                              alignment: Alignment.centerLeft,
                              margin: EdgeInsets.only(left: 63.w, top: 20.h),
                              child: Text(
                                Type.i18nNameBy(state.workoutModel.sportsType!),
                                style: TextStyle(color: Colors.black, fontSize: 49.sp),
                              ),
                            ),
                            Row(
                              children: [
                                SizedBox(width: 60.w),
                                buildSummaryInfo(),
                                const Spacer(),
                                Visibility(
                                  visible: avatar != '',
                                  replacement: Container(
                                    margin: EdgeInsets.only(top: 42.h),
                                    alignment: Alignment.center,
                                    width: 154.w,
                                    height: 154.w,
                                    child: Image.asset('assets/images/space/icon_avatar_default.png'),
                                  ),
                                  child: Container(
                                    width: 154.w,
                                    height: 154.w,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(50),
                                      image: DecorationImage(fit: BoxFit.fill, image: FileImage(File(avatar))),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                            SizedBox(height: 20.h),
                            Row(
                              children: [
                                SizedBox(width: 60.w),
                                Text(
                                  state.workoutModel.startTimeFormat,
                                  style: TextStyle(color: ColorConst.secondaryFontColor, fontSize: 37.sp),
                                ),
                                const Spacer(),
                                Text(
                                  name,
                                  style: TextStyle(color: ColorConst.secondaryFontColor, fontSize: 37.sp),
                                ),
                                SizedBox(width: 26.w),
                              ],
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 24.h, bottom: 50.h),
                              child: Divider(height: 1.h),
                            ),
                            SizedBox(height: 20.h),
                            buildSportsInfoContainer(),
                          ],
                        ),
                      ),
                      SizedBox(height: 20.h),
                      buildRateItem(),
                      SizedBox(height: 20.h),
                      buildSpeedItem(),
                      SizedBox(height: 20.h),
                      buildCadenceItem(),
                      SizedBox(height: 20.h),
                      // buildHeightItem(),
                      // SizedBox(height: 40.h),
                    ],
                  ),
                ),
              ),
            ),
          ],
        );
      },
    );
  }

  Widget buildSportsInfoContainer() {
    return Container(
      padding: EdgeInsets.only(left: 60.w, bottom: 0),
      child: GridView(
        physics: const NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,
          mainAxisSpacing: 10.w,
          crossAxisSpacing: 80.w,
          childAspectRatio: 2.8,
        ),
        children: [
          buildSportInfoItem(
            'assets/images/sports/icon_sports_time.png',
            LocaleKeys.sports_goal_duration_text.ts,
            DateTool.second2HMS(state.workoutModel.duration ?? 0),
            '',
          ),
          if (logic.isDistanceSports())
            buildSportInfoItem(
              'assets/images/sports/icon_sports_calorie.png',
              LocaleKeys.sports_calories_text.ts,
              '${state.workoutModel.caloriesFormat.split(' ')[0]}',
              LocaleKeys.thermal_unit_kcal.ts,
            ),
          if (logic.isDistanceSports())
            buildSportInfoItem(
              'assets/images/sports/icon_sports_speed.png',
              LocaleKeys.sports_pace_text.ts,
              logic.paceAverage(),
              '/ ${logic.getLengthUnitString()}',
            ),
          if (logic.isStepSports())
            buildSportInfoItem(
              'assets/images/sports/icon_sports_cadence.png',
              LocaleKeys.sports_cadence.ts,
              '${logic.cadenceAverage()}',
              '${LocaleKeys.step.ts}/${LocaleKeys.sports_goal_duration_unit_text.ts}',
            ),
          if (logic.isStepSports())
            buildSportInfoItem(
              'assets/images/sports/icon_sports_stride.png',
              LocaleKeys.sports_stride.ts,
              '${logic.getStride()}',
              LocaleKeys.sports_cm.ts,
            ),
          if (logic.isStepSports())
            buildSportInfoItem(
              'assets/images/sports/icon_sports_step.png',
              LocaleKeys.sports_step_count_text.ts,
              '${state.workoutModel.step}',
              LocaleKeys.step.ts,
            ),
          buildSportInfoItem(
            'assets/images/sports/icon_sports_rate.png',
            LocaleKeys.widgets_heart_rate.ts,
            '${logic.heartRateAverage()}',
            '/${LocaleKeys.sports_goal_duration_unit_text.ts}',
          ),
        ],
      ),
    );
  }

  Widget map() {
    if (logic.hiddenMap()) {
      return Container();
    }

    return Container(
      width: 1.sw,
      height: 1.sh - 820.h,
      margin: EdgeInsets.fromLTRB(38.w, 29.h, 38.w, 0.h),
      decoration: BoxDecoration(
        boxShadow: [BoxShadow(color: Colors.grey.withOpacity(0.7), blurRadius: 5)],
        borderRadius: BorderRadius.circular(60.r),
      ),
      child: GetBuilder<MotionEndLogic>(
        id: 'motion_end_map',
        builder: (_) => GoogleMap(
          mapToolbarEnabled: false,
          zoomControlsEnabled: false,
          mapType: MapType.normal,
          initialCameraPosition: const CameraPosition(target: LatLng(0, 0), zoom: 15.0),
          myLocationEnabled: false,
          myLocationButtonEnabled: false,
          polylines: {state.polyLine},
          markers: Set.from(state.markers),
          onMapCreated: (controller) => logic.onMapCreated(controller),
        ),
      ),
    );
  }

  Widget buildSportInfoItem(String asset, String itemName, String itemValue, String unit) {
    return Stack(
      alignment: AlignmentDirectional.topEnd,
      children: [
        Positioned(
          left: 10.w,
          top: itemName == LocaleKeys.sports_cadence.ts ? 50.w : 25.w,
          child: Image.asset(asset, width: 59.w, height: 59.w),
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              width: 360.w,
              child: Text(
                itemName,
                softWrap: false,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(color: ColorConst.secondaryFontColor, fontSize: 42.sp, height: 0.9),
                textAlign: TextAlign.left,
                maxLines: 2,
              ),
            ),
            SizedBox(height: 10.h),
            RichText(
              text: TextSpan(children: [
                TextSpan(
                  text: itemValue,
                  style: TextStyle(fontSize: 52.sp, color: Colors.black),
                ),
                TextSpan(
                  text: ' $unit',
                  style: TextStyle(fontSize: 42.sp, color: ColorConst.secondaryFontColor),
                ),
              ]),
            ),
          ],
        ),
      ],
    );
  }

  /// 心率图表和区间比例
  Widget buildRateItem() {
    return Container(
      margin: EdgeInsets.only(left: 38.w, right: 38.w),
      padding: EdgeInsets.only(left: 60.w, top: 50.h, bottom: 42.h),
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(40.r)),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          RichText(
            text: TextSpan(children: [
              TextSpan(
                text: LocaleKeys.measure_heart.ts,
                style: TextStyle(fontSize: 52.sp, color: Colors.black),
              ),
              TextSpan(
                text: '  ${LocaleKeys.exercise_times_text.ts}/${LocaleKeys.sports_goal_duration_unit_text.ts}',
                style: TextStyle(fontSize: 42.sp, color: ColorConst.secondaryFontColor),
              ),
            ]),
          ),
          SizedBox(height: 20.h),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Column(
                children: [
                  Text(LocaleKeys.sports_hr_max.ts,
                      style: TextStyle(color: ColorConst.secondaryFontColor, fontSize: 42.sp), textAlign: TextAlign.center),
                  Text('${logic.heartRateMax()}', style: TextStyle(color: Colors.black, fontSize: 52.sp), textAlign: TextAlign.center),
                ],
              ),
              Column(
                children: [
                  Text(LocaleKeys.sports_hr_min.ts,
                      style: TextStyle(color: ColorConst.secondaryFontColor, fontSize: 42.sp), textAlign: TextAlign.center),
                  Text('${logic.heartRateMin()}', style: TextStyle(color: Colors.black, fontSize: 52.sp), textAlign: TextAlign.center),
                ],
              ),
            ],
          ),
          ChartView(chartList: state.hrMapList),
          SizedBox(height: 50.h),
          Row(
            children: [
              SizedBox(
                height: 334.w,
                width: 334.w,
                child: AspectRatio(
                  aspectRatio: 1,
                  child: PieChart(
                    PieChartData(
                      borderData: FlBorderData(show: false),
                      sectionsSpace: state.hrMapList.isEmpty ? 0 : 1.w,
                      centerSpaceRadius: 130.w,
                      sections: showingSections(),
                    ),
                  ),
                ),
              ),
              const Spacer(),
              SizedBox(
                width: 545.w,
                child: Column(
                  children: [
                    Row(
                      children: [
                        Container(
                          width: 15.w,
                          height: 15.w,
                          margin: EdgeInsets.only(right: 19.h),
                          decoration: BoxDecoration(color: const Color(0xFFFD4B59), borderRadius: BorderRadius.circular(20)),
                        ),
                        Text(
                          LocaleKeys.sports_limit.ts,
                          style: TextStyle(color: Colors.black, fontSize: 42.sp),
                        ),
                        const Spacer(),
                        Text(state.limitPercentText, style: TextStyle(color: Colors.black, fontSize: 42.sp)),
                        SizedBox(width: 10.w),
                      ],
                    ),
                    Row(
                      children: [
                        Container(
                          width: 15.w,
                          height: 15.w,
                          margin: EdgeInsets.only(right: 19.h),
                          decoration: BoxDecoration(color: const Color(0xFFFD652A), borderRadius: BorderRadius.circular(20)),
                        ),
                        SizedBox(
                          width: 350.w,
                          child: Text(
                            LocaleKeys.sports_endurance.ts,
                            style: TextStyle(color: Colors.black, fontSize: 42.sp, overflow: TextOverflow.ellipsis),
                          ),
                        ),
                        const Spacer(),
                        Text(state.endurancePercentText, style: TextStyle(color: Colors.black, fontSize: 42.sp)),
                        SizedBox(width: 10.w),
                      ],
                    ),
                    Row(
                      children: [
                        Container(
                          width: 15.w,
                          height: 15.w,
                          margin: EdgeInsets.only(right: 19.h),
                          decoration: BoxDecoration(color: const Color(0xFFFEB72B), borderRadius: BorderRadius.circular(20)),
                        ),
                        SizedBox(
                          width: 350.w,
                          child: Text(
                            LocaleKeys.sports_anaerobic.ts,
                            style: TextStyle(color: Colors.black, fontSize: 42.sp, overflow: TextOverflow.ellipsis),
                          ),
                        ),
                        const Spacer(),
                        Text(state.anaerobicPercentText, style: TextStyle(color: Colors.black, fontSize: 42.sp)),
                        SizedBox(width: 10.w),
                      ],
                    ),
                    Row(
                      children: [
                        Container(
                          width: 15.w,
                          height: 15.w,
                          margin: EdgeInsets.only(right: 19.h),
                          decoration: BoxDecoration(color: const Color(0xFF2AFD72), borderRadius: BorderRadius.circular(20)),
                        ),
                        Text(
                          LocaleKeys.sports_fat.ts,
                          style: TextStyle(color: Colors.black, fontSize: 42.sp),
                        ),
                        const Spacer(),
                        Text(state.fatPercentText, style: TextStyle(color: Colors.black, fontSize: 42.sp)),
                        SizedBox(width: 10.w),
                      ],
                    ),
                    Row(
                      children: [
                        Container(
                          width: 15.w,
                          height: 15.w,
                          margin: EdgeInsets.only(right: 19.h),
                          decoration: BoxDecoration(color: Colors.lightBlueAccent, borderRadius: BorderRadius.circular(20)),
                        ),
                        Text(
                          LocaleKeys.sports_warm.ts,
                          style: TextStyle(color: Colors.black, fontSize: 42.sp),
                        ),
                        const Spacer(),
                        Text(state.warmPercentText, style: TextStyle(color: Colors.black, fontSize: 42.sp)),
                        SizedBox(width: 10.w),
                      ],
                    ),
                  ],
                ),
              ),
              SizedBox(
                width: 60.w,
              )
            ],
          ),
        ],
      ),
    );
  }

  Widget buildSpeedItem() {
    return !logic.isNeedPaceChart()
        ? Container()
        : Container(
            margin: EdgeInsets.only(left: 38.w, right: 38.w),
            padding: EdgeInsets.only(
              left: 60.w,
              top: 50.h,
            ),
            decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(40.r)),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                RichText(
                  text: TextSpan(children: [
                    TextSpan(
                      text: LocaleKeys.exercise_pace_text.ts,
                      style: TextStyle(fontSize: 52.sp, color: Colors.black),
                    ),
                    TextSpan(
                      text: '  ${LocaleKeys.sports_goal_duration_unit_text.ts}/${logic.getLengthUnitString()}',
                      style: TextStyle(fontSize: 42.sp, color: ColorConst.secondaryFontColor),
                    ),
                  ]),
                ),
                SizedBox(
                  height: 20.h,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Column(
                      children: [
                        Text(LocaleKeys.sports_pace_max.ts,
                            style: TextStyle(color: ColorConst.secondaryFontColor, fontSize: 42.sp), textAlign: TextAlign.center),
                        Text(logic.paceMin(), style: TextStyle(color: Colors.black, fontSize: 52.sp), textAlign: TextAlign.center),
                      ],
                    ),
                    Column(
                      children: [
                        Text(LocaleKeys.sports_pace_text.ts,
                            style: TextStyle(color: ColorConst.secondaryFontColor, fontSize: 42.sp), textAlign: TextAlign.center),
                        Text(logic.paceAverage(), style: TextStyle(color: Colors.black, fontSize: 52.sp), textAlign: TextAlign.center),
                      ],
                    ),
                  ],
                ),
                ChartView(
                  chartList: state.paceMapList,
                  gradientColors: const [
                    Color.fromRGBO(0, 245, 101, 1),
                    Colors.white60,
                  ],
                ),
              ],
            ),
          );
  }

  Widget buildCadenceItem() {
    return !logic.isNeedCadenceChart()
        ? Container()
        : Container(
            margin: EdgeInsets.only(left: 38.w, right: 38.w),
            padding: EdgeInsets.only(
              left: 60.w,
              top: 50.h,
            ),
            decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(40.r)),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                RichText(
                  text: TextSpan(children: [
                    TextSpan(
                      text: LocaleKeys.sports_cadence.ts,
                      style: TextStyle(fontSize: 52.sp, color: Colors.black),
                    ),
                    TextSpan(
                      text: '  ${LocaleKeys.step.ts}/${LocaleKeys.sports_goal_duration_unit_text.ts}',
                      style: TextStyle(fontSize: 42.sp, color: ColorConst.secondaryFontColor),
                    ),
                  ]),
                ),
                SizedBox(
                  height: 20.h,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Column(
                      children: [
                        Text(LocaleKeys.sports_cadence_max.ts,
                            style: TextStyle(color: ColorConst.secondaryFontColor, fontSize: 42.sp), textAlign: TextAlign.center),
                        Text('${logic.cadenceMax()}', style: TextStyle(color: Colors.black, fontSize: 52.sp), textAlign: TextAlign.center),
                      ],
                    ),
                    Column(
                      children: [
                        Text(LocaleKeys.sports_cadence_average.ts,
                            style: TextStyle(color: ColorConst.secondaryFontColor, fontSize: 42.sp), textAlign: TextAlign.center),
                        Text('${logic.cadenceAverage()}', style: TextStyle(color: Colors.black, fontSize: 52.sp), textAlign: TextAlign.center),
                      ],
                    ),
                  ],
                ),
                ChartView(chartList: state.cadenceMapList, gradientColors: const [Color.fromRGBO(255, 184, 3, 1), Colors.white]),
              ],
            ),
          );
  }

  Widget buildHeightItem() {
    return !logic.isNeedAltitudeChart()
        ? Container()
        : Container(
            margin: EdgeInsets.only(left: 38.w, right: 38.w),
            padding: EdgeInsets.only(
              left: 60.w,
              top: 50.h,
            ),
            decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(40.r)),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                RichText(
                  text: TextSpan(children: [
                    TextSpan(
                      text: LocaleKeys.sports_altitude.ts,
                      style: TextStyle(fontSize: 52.sp, color: Colors.black),
                    ),
                    TextSpan(
                      text: '  ${LocaleKeys.length_unit_m.ts}',
                      style: TextStyle(fontSize: 42.sp, color: ColorConst.secondaryFontColor),
                    ),
                  ]),
                ),
                SizedBox(
                  height: 20.h,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Column(
                      children: [
                        Text(LocaleKeys.sports_altitude_min.ts,
                            style: TextStyle(color: ColorConst.secondaryFontColor, fontSize: 42.sp), textAlign: TextAlign.center),
                        Text('${logic.altitudeMin()}', style: TextStyle(color: Colors.black, fontSize: 52.sp), textAlign: TextAlign.center),
                      ],
                    ),
                    Column(
                      children: [
                        Text(LocaleKeys.sports_altitude_max.ts,
                            style: TextStyle(color: ColorConst.secondaryFontColor, fontSize: 42.sp), textAlign: TextAlign.center),
                        Text('${logic.altitudeMax()}', style: TextStyle(color: Colors.black, fontSize: 52.sp), textAlign: TextAlign.center),
                      ],
                    ),
                  ],
                ),
                ChartView(chartList: state.altitudeMapList, gradientColors: const [Color.fromRGBO(39, 140, 255, 1), Colors.white]),
              ],
            ),
          );
  }

  getAvatarContainer(String? avatar) {
    return avatar != null && avatar.isNotEmpty
        ? Container(
            width: 154.w,
            height: 154.w,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(50),
              image: DecorationImage(fit: BoxFit.fill, image: FileImage(File(avatar))),
            ))
        : SizedBox(width: 154.w, height: 154.w, child: const Icon(Icons.person));
  }

  showingSections() {
    int total = state.heartRateList.length;

    double limitPercent = state.limitPercent;
    double endurancePercent = state.endurancePercent;
    double anaerobicPercent = state.anaerobicPercent;
    double fatPercent = state.fatPercent;
    double warmPercent = state.warmPercent;

    double width = 40.w;
    if (total <= 0 || (limitPercent <= 0 && endurancePercent <= 0 && anaerobicPercent <= 0 && fatPercent <= 0 && warmPercent <= 0)) {
      return [
        PieChartSectionData(radius: width, color: Colors.grey.withOpacity(.3), value: 25, title: ''),
        PieChartSectionData(radius: width, color: Colors.grey.withOpacity(.3), value: 25, title: ''),
        PieChartSectionData(radius: width, color: Colors.grey.withOpacity(.3), value: 25, title: ''),
        PieChartSectionData(radius: width, color: Colors.grey.withOpacity(.3), value: 25, title: ''),
        PieChartSectionData(radius: width, color: Colors.grey.withOpacity(.3), value: 25, title: ''),
      ];
    }

    return [
      PieChartSectionData(radius: width, color: const Color(0xFFFD4B59), value: limitPercent, title: ''),
      PieChartSectionData(radius: width, color: const Color(0xFFFD652A), value: endurancePercent, title: ''),
      PieChartSectionData(radius: width, color: const Color(0xFFFEB72B), value: anaerobicPercent, title: ''),
      PieChartSectionData(radius: width, color: const Color(0xFF2AFD72), value: fatPercent, title: ''),
      PieChartSectionData(radius: width, color: Colors.lightBlueAccent, value: warmPercent, title: ''),
    ];
  }
}
